home *** CD-ROM | disk | FTP | other *** search
/ Programming an RTS Game with Direct3D / Programming an RTS Game with Direct3D.iso / Examples / Chapter 4 / Example 4.11 / mesh.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2006-07-01  |  2.8 KB  |  127 lines

  1. #include "mesh.h"
  2.  
  3. MESH::MESH()
  4. {
  5.     m_pDevice = NULL;
  6.     m_pMesh = NULL;
  7. }
  8.  
  9. MESH::MESH(char fName[], IDirect3DDevice9* Dev)
  10. {
  11.     m_pDevice = Dev;
  12.     m_pMesh = NULL;
  13.     Load(fName, m_pDevice);
  14. }
  15.  
  16. MESH::~MESH()
  17. {
  18.     Release();
  19. }
  20.  
  21. HRESULT MESH::Load(char fName[], IDirect3DDevice9* Dev)
  22. {
  23.     m_pDevice = Dev;
  24.  
  25.     //Set m_white material
  26.     m_white.Ambient = m_white.Specular = m_white.Diffuse  = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
  27.     m_white.Emissive = D3DXCOLOR(0.0f, 0.0f, 0.0f, 1.0f);
  28.     m_white.Power = 1.0f;
  29.  
  30.     Release();
  31.  
  32.     //Load new m_pMesh
  33.     ID3DXBuffer * adjacencyBfr = NULL;
  34.     ID3DXBuffer * materialBfr = NULL;
  35.     DWORD noMaterials = NULL;
  36.  
  37.     if(FAILED(D3DXLoadMeshFromX(fName, D3DXMESH_MANAGED, m_pDevice,    
  38.                                 &adjacencyBfr, &materialBfr, NULL, 
  39.                                 &noMaterials, &m_pMesh)))
  40.         return E_FAIL;
  41.  
  42.     D3DXMATERIAL *mtrls = (D3DXMATERIAL*)materialBfr->GetBufferPointer();
  43.  
  44.     for(int i=0;i<noMaterials;i++)
  45.     {
  46.         //mtrls[i].MatD3D.Ambient = mt[i].MatD3D.Diffuse;
  47.         m_materials.push_back(mtrls[i].MatD3D);
  48.  
  49.         if(mtrls[i].pTextureFilename != NULL)
  50.         {
  51.             char textureFileName[90];
  52.             strcpy(textureFileName, "meshes/");
  53.             strcat(textureFileName, mtrls[i].pTextureFilename);
  54.             IDirect3DTexture9 * newTexture = NULL;
  55.             D3DXCreateTextureFromFile(m_pDevice, textureFileName, &newTexture);            
  56.             m_textures.push_back(newTexture);
  57.         }
  58.         else m_textures.push_back(NULL);
  59.     }
  60.  
  61.     m_pMesh->OptimizeInplace(D3DXMESHOPT_ATTRSORT | D3DXMESHOPT_COMPACT | D3DXMESHOPT_VERTEXCACHE,
  62.                             (DWORD*)adjacencyBfr->GetBufferPointer(), NULL, NULL, NULL);
  63.  
  64.     adjacencyBfr->Release();
  65.     materialBfr->Release();
  66.  
  67.     return S_OK;
  68. }
  69.  
  70. void MESH::Render()
  71. {
  72.     for(int i=0;i<m_materials.size();i++)
  73.     {    
  74.         if(m_textures[i] != NULL)m_pDevice->SetMaterial(&m_white);
  75.         else m_pDevice->SetMaterial(&m_materials[i]);
  76.         m_pDevice->SetTexture(0,m_textures[i]);
  77.         m_pMesh->DrawSubset(i);
  78.     }    
  79. }
  80.  
  81. void MESH::Release()
  82. {
  83.     //Clear old mesh...
  84.     if(m_pMesh != NULL)
  85.     {
  86.         m_pMesh->Release();
  87.         m_pMesh = NULL;
  88.     }
  89.  
  90.     //Clear textures and materials
  91.     for(int i=0;i<m_textures.size();i++)
  92.         if(m_textures[i] != NULL)
  93.             m_textures[i]->Release();
  94.  
  95.     m_textures.clear();
  96.     m_materials.clear();    
  97. }
  98.  
  99. MESHINSTANCE::MESHINSTANCE()
  100. {
  101.     m_pMesh = NULL;
  102.     m_pos = m_rot = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
  103.     m_sca = D3DXVECTOR3(1.0f, 1.0f, 1.0f);
  104. }
  105.  
  106. MESHINSTANCE::MESHINSTANCE(MESH *meshPtr)
  107. {
  108.     m_pMesh = meshPtr;
  109.     m_pos = m_rot = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
  110.     m_sca = D3DXVECTOR3(1.0f, 1.0f, 1.0f);
  111. }
  112.  
  113. void MESHINSTANCE::Render()
  114. {
  115.     if(m_pMesh != NULL)
  116.     {
  117.         D3DXMATRIX p, r, s;
  118.         D3DXMatrixTranslation(&p, m_pos.x, m_pos.y, m_pos.z);
  119.         D3DXMatrixRotationYawPitchRoll(&r, m_rot.y, m_rot.x, m_rot.z);
  120.         D3DXMatrixScaling(&s, m_sca.x, m_sca.y, m_sca.z);
  121.         
  122.         D3DXMATRIX world = s * r * p;
  123.         m_pMesh->m_pDevice->SetTransform(D3DTS_WORLD, &world);
  124.  
  125.         m_pMesh->Render();
  126.     }
  127. }